<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['job:guardian:qualifications:add']"
          >新增</el-button
        >
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #actionButton="{ row }">
        <el-button type="text" @click="handleDownLoad(row.fileUrl)"
          >证照</el-button
        >
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['job:guardian:qualifications:edit']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['job:guardian:qualifications:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :title="dialogType == 'add' ? '新增监护人' : '修改监护人'"
      :slots="['fileSlot']"
    >
      <template #fileSlot>
        <el-form-item label="证照附件">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="files"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/zygl/jhrwh.js";
import formJson from "./components/options.json";
import FileUpload from "@/components/FileUpload";

import { useAsyncData, useFormJsonNew } from "@/views/hooks/useAsyncData.js";
const { proxy } = getCurrentInstance();

const ops = useFormJsonNew(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops.filter((i) => !["fileSlot"].includes(i.prop)),
  formOps: ops.filter((i) =>
    ["hrCard", "workShop", "name", "izQualified", "cardType"].includes(i.prop)
  ),
});
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();
const files = ref([]);
watch(
  () => files,
  (newData) => {
    console.log(newData);
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const plugEdit = (row) => {
  if (row.fileId) {
    files.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
  handleDownLoad,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy, plugEdit });
</script>

<style lang="scss" scoped></style>
